﻿// <copyright file="TestRunnerProcessor.cs" company="T4 Toolbox Team">
//  Copyright © T4 Toolbox Team. All Rights Reserved.
// </copyright>

namespace T4Toolbox
{
    using System.CodeDom;
    using System.Collections.Generic;
    using Microsoft.VisualStudio.TextTemplating;

    /// <summary>
    /// This directive processor is a part of the T4 Toolbox infrastructure. Don't
    /// use it in your templates.
    /// </summary>
    /// <remarks>
    /// This directive generates code necessary to automatically execute
    /// <see cref="TestRunner.RunTests()"/> method during template transformation during 
    /// <see cref="TextTransformation.Initialize"/>. It is used in UnitTesting.tt, included 
    /// by T4 unit test files to execute all test methods.
    /// </remarks>
    public class TestRunnerProcessor : DirectiveProcessor
    {
        /// <summary>
        /// Gets the directive name as it is supposed to be used in template code.
        /// </summary>
        /// <value>
        /// A <see cref="string"/> that contains name of the TestRunner directive.
        /// </value>
        protected override string DirectiveName
        {
            get { return "TestRunner"; }
        }

        /// <summary>
        /// Generates invoke <see cref="TestRunner.RunTests()"/> method at the end of 
        /// <see cref="TextTransformation.Initialize"/>.
        /// </summary>
        /// <param name="directiveName">
        /// The name of the directive to process. 
        /// </param>
        /// <param name="arguments">
        /// The arguments for the directive. 
        /// </param>
        public override void ProcessDirective(string directiveName, IDictionary<string, string> arguments)
        {
            base.ProcessDirective(directiveName, arguments);

            //// TestRunner.RunTests();
            CodeExpressionStatement runTests = new CodeExpressionStatement(
                new CodeMethodInvokeExpression(
                    new CodeTypeReferenceExpression(typeof(TestRunner).Name),
                    "RunTests"));

            this.LanguageProvider.GenerateCodeFromStatement(runTests, this.PostInitializationCode, null);
        }
    }
}
